﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CourseUtils;

namespace factorialWithinFileAndFolder
{
    class Program
    {
        static int monitor()
        {
            CourseUtils.IOHelpers.PrintString2Console("Выберите желаемое действие:\n1.Создать дерево папок и файлов.\n2.Вычислить факториал рекурсивным методом.\n3.Вычислить факториал итерационным методом.", "");
            int choise = CourseUtils.IOHelpers.ReadIntFromConsole("\n введите номер действия 1 или 2 или 3");
            return choise;
        }
        static void createFilesAndFolders(string fullText)
        {
            Console.WriteLine("case 1");

            int i = 0;
            Char c = 'A';
            String extention = "xxx";
            while (i <= 10)
            {
                Directory.CreateDirectory(Environment.CurrentDirectory + "\\" + i);
                ushort serialNumberOfFile = 0;
                while (c <= 'Z')
                {
                    serialNumberOfFile++; 
                    string path = String.Format("{3}\\{0}\\{1}.{2}", i, c, extention, Environment.CurrentDirectory);
                    FileStream fs = File.Create(path);
                    DateTime fileCreatedDate = File.GetCreationTime(path);
                    string myString = serialNumberOfFile.ToString();                    
                    string text = String.Format("Serial number of file = {0}\n  DateTime = {1},\n  factorial = {2}", myString, fileCreatedDate, fullText);
                    Console.WriteLine(text);
                    fs.Close();
                    File.AppendAllText(path, text);                    
                  //  File.WriteAllText(path, "----");
                    c++;
                }
                c = 'A';
                i++;
            }
            return;
        }
        static int insetNumberForFactorialCalculation()
        {
            int number = CourseUtils.IOHelpers.ReadIntFromConsole("\nВведите число для вычисления факториала от 0 до 20", 0, 20);
            Console.WriteLine("\n введено число равное {0}", number);
            return number;
        }
        static ulong recursFactorial(int num)
        {
            ulong factorial;
            if (num == 0)
            {
                factorial = 1;
            }
            else
            {               
                    factorial = (ulong)num * recursFactorial(num - 1);
            }
            return factorial;
        }
        static ulong factorialByRecursion()
        {
            int num = insetNumberForFactorialCalculation();
            ulong factorial = recursFactorial(num);
            return factorial;
        }
        static ulong factorialByIteration()
        {
            int num = insetNumberForFactorialCalculation();
            ulong factorial;
            if (num == 0)
            {
                factorial = 1;
            }
            else
            {
                factorial = 1;
                for (int counter = num; counter >= 1; counter--)
                {
                    factorial *= (ulong)counter;
                }
            }
            return factorial;
        }
  /*      static insetInFileSequenceNumberAndCreationDateAndFactorial(string factorial)
        {

        } */
        static void Main(string[] args)
        {
            int action = monitor();
            switch (action) {
                case 1: createFilesAndFolders("");
                    break;
                case 2: 
                    {                    
                    Console.WriteLine("case 2");
                    ulong result = factorialByRecursion();
                    string factorial = result.ToString();
                    CourseUtils.IOHelpers.PrintString2Console(factorial, "Факториал числа равен ");
                    createFilesAndFolders(factorial);
                    }
                    break;
                case 3:
                    {
                        Console.WriteLine("case 3");
                        ulong result = factorialByIteration();
                        string factorial = result.ToString();
                        CourseUtils.IOHelpers.PrintString2Console(factorial, "Факториал числа равен ");
                        createFilesAndFolders(factorial);
                    }
                    break;
            }

            Console.ReadKey();
        }
    }
}

